<?php

namespace App\Traits;
use App\Models\SysConfig as Setter;

trait Config{
    public function setConfig($key,$value,$group=''){
        $setter = Setter::where('key',$key)->first();
        if(is_null($setter)){
            $setter = new Setter;
            $setter->key = $key;
            $setter->value = $value;
            $setter->group = $group;
            $setter->save();
            return $setter->value;
        }else{
            return $setter->value;
        }
    }

    public function config($key,$default='',$group = false){
        $setter = Setter::where('key',$key)
                ->when($group,function($q) use($group){
                    $q->where('group',$group);
                })->first();
        if(is_null($setter)){
            $setter = new Setter;
            $setter->key = $key;
            $setter->value = $default;
            if($group){
                $setter->group = $group;
            }
            $setter->save();
            return $default;
        }else{
            return $setter->value;
        }
    }

    public function groupConfig($group){
        $setter = Setter::where('group',$group)->get();
        if($setter->isEmpty()){
            return false;
        }else{
            return $setter;
        }
    }
}
